package search

import "fmt"

// mid自适应
// mid = low + (end-start) * (target-arr[end]) / (arr[end]-arr[start])
// 实际上表示low索引加上间隔比例

// BinarySearch 二分查找
func BinarySearch(arr []int, target int) int {
	return sea1(arr, target, 0, len(arr)-1)
}

func sea1(arr []int, target, start, end int) int {
	counter := 0
	for midPtr := (start + end) >> 1; start <= end; midPtr = (start + end) >> 1 {
		counter++
		if arr[midPtr] == target {
			fmt.Println(counter)
			return midPtr
		}
		if arr[midPtr] > target {
			end = midPtr - 1
		} else {
			start = midPtr + 1
		}
	}

	fmt.Println(counter)
	return -1
}
